package com.mz.user.api.impl.controller;



import com.mz.common.domain.SysMenu;
import com.mz.common.domain.SysUser;
import com.mz.common.page.PageList;
import com.mz.common.utils.ResponseResult;
import com.mz.user.api.impl.service.SysUserService;
import com.mz.user.api.query.UserQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 个人信息管理
 * <p>

 */
@RestController
@RequestMapping(value = "usercenter")
public class UserCenterController {

    @Autowired
    private SysUserService sysUserService;

    @Resource
    private BCryptPasswordEncoder passwordEncoder;


    /**
     * 获取所有用户的信息(分页)
     *
     * @return {@link ResponseResult}
     */
    @GetMapping(value = "page")
    public ResponseResult page(UserQuery userQuery) throws Exception {
        //返回用户对应的菜单 根据userid查询
        PageList<SysUser> pageList = sysUserService.findPage(userQuery);
        return new ResponseResult (ResponseResult.CodeStatus.OK, "获取用户信息", pageList);
    }

    @PostMapping(value = "/saveUser")
    public ResponseResult saveUser(@RequestBody SysUser sysUser) throws Exception {
        try {
            sysUserService.saveUser(sysUser);
            return  new ResponseResult<Map>(ResponseResult.CodeStatus.OK,
                    "保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  new ResponseResult<Map>(ResponseResult.CodeStatus.FAIL,
                    "保存失败");
        }
    }

    @PutMapping(value = "/updateUser")
    public ResponseResult updateUser(@RequestBody SysUser sysUser) throws Exception {
        try {
            sysUserService.updateUser(sysUser);
            return  new ResponseResult<Map>(ResponseResult.CodeStatus.OK,
                    "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  new ResponseResult<Map>(ResponseResult.CodeStatus.FAIL,
                    "修改失败");
        }
    }

    @DeleteMapping(value = "/deleteUser/{id}")
    public ResponseResult deleteUser(@PathVariable("id") Long id) throws Exception {
        try {
            sysUserService.deleteUserById(id);
            return  new ResponseResult<Map>(ResponseResult.CodeStatus.OK,
                    "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  new ResponseResult<Map>(ResponseResult.CodeStatus.FAIL,
                    "删除失败");
        }
    }

    //保存用户角色 saveUserRole


    @PostMapping(value = "/saveUserRole")
    public ResponseResult saveUserRole(@RequestBody List<Map> userRoleList) throws Exception {
        try {
            sysUserService.saveUserRole(userRoleList);
            return  new ResponseResult<Map>(ResponseResult.CodeStatus.OK,
                    "保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  new ResponseResult<Map>(ResponseResult.CodeStatus.FAIL,
                    "保存失败");
        }
    }


}
